package com.itlong.cloud.controller.system;

import com.itlong.cloud.POJO.DTO.operate.OperateFunctionGetPageDTO;
import com.itlong.cloud.POJO.PO.OperateFunctionPO;
import com.itlong.cloud.POJO.PO.OperateRolePO;
import com.itlong.cloud.POJO.PO.OperateUserInfoPO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.operate.OperateAllotFunctionGetVO;
import com.itlong.cloud.POJO.VO.operate.OperateFunctionVO;
import com.itlong.cloud.POJO.VO.operate.OperateGetRoleMenuVO;
import com.itlong.cloud.abstracts.BaseController;
import com.itlong.cloud.annotation.AuthAccessRequired;
import com.itlong.cloud.annotation.LoggerInfo;
import com.itlong.cloud.constants.LogDescConstant;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.enumerate.ecode.OperateErrorCodeEnum;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.user.operate.IOperateFunctionServiceClient;
import com.itlong.cloud.user.operate.IOperateUserInfoServiceClient;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import com.itlong.cloud.utils.page.Page;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <desc>
 *      运营-系统-菜单管理控制器
 * </desc>
 *
 * @createDate 2017/09/21
 */
@RestController
@RequestMapping("/system/function")
public class OperateFunctionController extends BaseController{
    private static final Logger LOG = LoggerFactory.getLogger(OperateFunctionController.class);
    @Autowired
    IOperateFunctionServiceClient iOperateFunctionServiceClient;

    @Autowired
    IOperateUserInfoServiceClient iOperateUserInfoServiceClient;
    /**
     * <desc>
     *      获取分页列表
     * </desc>
     *
     * @param operateFunctionGetPageDTO 查询参数DTO
     * @return
     * @author Jiaqi.X
     * @createDate 2017/09/21
     */
    @RequestMapping(path = "getPage",method = RequestMethod.POST)
    @ResponseBody
    @AuthAccessRequired
    public Object getFunctionPage(OperateFunctionGetPageDTO operateFunctionGetPageDTO){
        MessageVO messageVO;
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            operateFunctionGetPageDTO= HttpProtocolUtil.parseRequestParamToDTO(OperateFunctionGetPageDTO.class,request);
            Page<OperateFunctionPO> page=iOperateFunctionServiceClient.getPage(operateFunctionGetPageDTO);
            messageVO=new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),page);
        } catch (Exception e) {
            throw new DataAccessException("【运营_系统_资源菜单getPage】获取分页列表失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      添加资源
     * </desc>
     *
     * @param operateFunctionPO 资源实体
     * @return
     * @author Jiaqi.X
     * @createDate 2017/09/21
     */
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.FUNCTION_OPERATE_LOG_SAVE)
    @RequestMapping(path = "save",method = RequestMethod.POST)
    @ResponseBody
    public Object saveFunction(OperateFunctionPO operateFunctionPO){
        MessageVO messageVO;
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            operateFunctionPO= HttpProtocolUtil.parseRequestParamToDTO(OperateFunctionPO.class,request);
            if (operateFunctionPO.getIdentifier() == null) {
                operateFunctionPO.setIdentifier("");
            }
            iOperateFunctionServiceClient.save(operateFunctionPO);
            messageVO=new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        } catch (Exception e) {
            throw new DataAccessException("【运营_系统_资源菜单save】添加资源失败",e);
        }
        return messageVO;

    }

    /**
     * <desc>
     *      修改资源
     * </desc>
     *
     * @param operateFunctionPO 资源实体
     * @return
     * @author Jiaqi.X
     * @createDate 2017/09/21
     */
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.FUNCTION_OPERATE_LOG_UPDATE)
    @RequestMapping(path = "update",method = RequestMethod.POST)
    @ResponseBody
    public Object updateFunction(OperateFunctionPO operateFunctionPO){
        MessageVO messageVO;
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            operateFunctionPO= HttpProtocolUtil.parseRequestParamToDTO(OperateFunctionPO.class,request);
            iOperateFunctionServiceClient.update(operateFunctionPO);
            messageVO=new MessageVO(operateFunctionPO.getFunctionId(),BaseErrorCodeEnum.SUCCESS.getErrorCode());
        } catch (Exception e) {
            throw new DataAccessException("【运营-_系统_资源菜单update】更新资源失败", e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      查询单条资源
     * </desc>
     *
     * @param operateFunctionPO 资源实体
     * @return
     * @author Jiaqi.X
     * @createDate 2017/09/21
     */
    @RequestMapping(path = "getOne",method = RequestMethod.POST)
    @ResponseBody
    public Object getFunction(OperateFunctionPO operateFunctionPO){
        MessageVO messageVO;
        try{
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            operateFunctionPO= HttpProtocolUtil.parseRequestParamToDTO(OperateFunctionPO.class,request);
            operateFunctionPO=iOperateFunctionServiceClient.getOne(operateFunctionPO);
            messageVO=new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),operateFunctionPO);
        }catch (Exception e){
            throw new DataAccessException("【运营_系统_资源菜单getOne】获取单条资源失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      删除资源
     * </desc>
     *
     * @param functionIds 资源id数组字符串 ，号分隔
     * @return
     * @author Jiaqi.X
     * @createDate 2017/09/21
     */
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.FUNCTION_OPERATE_LOG_DELETE)
    @RequestMapping(path = "delete",method = RequestMethod.POST)
    @ResponseBody
    public Object deleteFunction(String functionIds){
        MessageVO messageVO;
        try {
            HttpServletRequest request= ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            functionIds=request.getAttribute("functionIds")==null?null:request.getAttribute("functionIds").toString();
            iOperateFunctionServiceClient.delete(functionIds);
            messageVO=new MessageVO(functionIds,BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch(Exception e){
            throw new DataAccessException("【运营_系统_资源菜单delete】删除资源失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      查询下级节点
     * </desc>
     *
     * @param functionId 资源id
     * @return
     * @author Jiaqi.X
     * @createDate 2017/09/21
     */
    @RequestMapping(path = "getChilds",method = RequestMethod.POST)
    @ResponseBody
    public Object getFunctionChilds(String functionId){
        MessageVO messageVO;
        try{
            HttpServletRequest request= ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            functionId=request.getAttribute("functionId")==null?null:request.getAttribute("functionId").toString();
            List<OperateFunctionVO> list=iOperateFunctionServiceClient.getChilds(functionId);
            messageVO=new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),list);
        }catch (Exception e){
            throw new DataAccessException("【运营_系统_资源菜单getChilds】查询下级节点失败",e);
        }
        return messageVO;
    }


    /**
     * <desc>
     *      启用或禁用
     * </desc>
     *
     * @param operateFunctionPO 资源实体
     * @return
     * @author Jiaqi.X
     * @createDate 2017/09/26
     */
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.FUNCTION_OPERATE_LOG_BANORUSE)
    @RequestMapping(path = "banOrUse",method = RequestMethod.POST)
    @ResponseBody
    public Object banOrUseFunction(OperateFunctionPO operateFunctionPO){
        MessageVO messageVO;
        try{
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            operateFunctionPO= HttpProtocolUtil.parseRequestParamToDTO(OperateFunctionPO.class,request);
            iOperateFunctionServiceClient.update(operateFunctionPO);
            messageVO=new MessageVO(operateFunctionPO.getFunctionId(),BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【运营_系统_资源菜单banOrUse】启用或禁用失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      获取分配菜单所有节点
     * </desc>
     *
     * @return
     * @author Jiaqi.X
     * @createDate 2017/09/26
     */
    @RequestMapping(path = "getAllotFunction",method = RequestMethod.POST)
    @ResponseBody
    public Object getAllotFunction(OperateRolePO operateRolePO){
        MessageVO messageVO=null;
        try{
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            operateRolePO= HttpProtocolUtil.parseRequestParamToDTO(OperateRolePO.class,request);
            List<OperateAllotFunctionGetVO> list=iOperateFunctionServiceClient.getAllotFunction(operateRolePO.getRoleId());
            messageVO=new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),list);
        }catch (Exception e){
            throw new DataAccessException("【运营_系统_资源菜单getAllotFunction】获取分配菜单所有节点失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      获取当前节点的父级节点以及同级别所有父级节点
     * </desc>
     *
     * @param parent 资源id
     * @return
     * @author Jiaqi.X
     * @createDate 2017/09/30
     */
    @RequestMapping(path = "getParents",method = RequestMethod.POST)
    @ResponseBody
    public Object getParentLevelFunctions(String parent){
        MessageVO messageVO;
        try{
            HttpServletRequest request= ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            parent=request.getAttribute("parent")==null?null:request.getAttribute("parent").toString();
            List<OperateFunctionPO> list=iOperateFunctionServiceClient.getParentLevelFunctions(parent);
            messageVO=new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),list);
        }catch (Exception e){
            throw new DataAccessException("【运营_系统_资源菜单getParentLevelFunctions】获取当前节点的父级节点以及同级别所有父级节点失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      获取用户角色的菜单
     * </desc>
     *
     * @param token
     * @return
     * @author Jiaqi.X
     * @createDate 2017/10/17
     */
    @RequestMapping(path = "getMenus")
    @ResponseBody
    public Object getMenus(String token){
        HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        token= request.getAttribute("token")==null?null:request.getAttribute("token").toString();
        OperateUserInfoPO userInfoPO=iOperateUserInfoServiceClient.retrieve(token);
        MessageVO messageVO;
        try{
            if(userInfoPO.getRoleId()!=null) {
                List<OperateGetRoleMenuVO> menus = iOperateFunctionServiceClient.getRoleMenu(userInfoPO.getRoleId());
                messageVO = new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), menus);
            }else{
                messageVO = new MessageVO(OperateErrorCodeEnum.USER_AUTH_ERROR.getErrorCode());
            }
        }catch (Exception e){
            throw new DataAccessException("【运营_系统_资源菜单getMenus】获取用户角色的菜单失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      获取用户角色的按钮权限
     * </desc>
     *
     * @param functionId    当前点击菜单id
     * @return
     * @author Jiaqi.X
     * @createDate 2017/10/17
     */
    @RequestMapping(path = "getAuthBtns")
    @ResponseBody
    public Object getAuthBtns(String token,String functionId){
        MessageVO messageVO;
        HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        token= request.getAttribute("token")==null?null:request.getAttribute("token").toString();
        functionId= request.getAttribute("functionId")==null?null:request.getAttribute("functionId").toString();
        OperateUserInfoPO userInfoPO=iOperateUserInfoServiceClient.retrieve(token);
        try{
            if(userInfoPO.getRoleId()!=null) {
                List<OperateGetRoleMenuVO> btns = iOperateFunctionServiceClient.getAuthBtns(userInfoPO.getRoleId(), functionId);
                messageVO = new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), btns);
            }else{
                messageVO = new MessageVO(OperateErrorCodeEnum.USER_AUTH_ERROR.getErrorCode());
            }
        }catch (Exception e){
            throw new DataAccessException("【运营_系统_资源菜单getAuthBtns】获取用户角色的按钮权限",e);
        }
        return messageVO;
    }
}
